package sort;

import util.RandomUtil;

import java.util.Arrays;

public class Sort {


    public static void main(String[] args) {
        int[] arr = RandomUtil.randomArr();
        System.out.println(Arrays.toString(arr));
        quickSort(arr, 0, arr.length - 1);
        System.out.println(Arrays.toString(arr));
    }


    //快速排序
    public static void quickSort(int[] arr, int left, int right) {

        if (left < right) {
            int l = left;
            int r = right;
            int x = arr[left];
            while (l < r) {
                while (l < r && arr[r] >= x)
                    r--;
                if (l < r) {
                    arr[l++] = arr[r];
                }
                while (l < r && arr[l] < x)
                    l++;
                if (l < r) {
                    arr[r--] = arr[l];
                }

            }
            arr[l] = x;
            quickSort(arr, left, l - 1);

            quickSort(arr, l + 1, right);
        }

    }

    //选择排序
    public static void selectionSort(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            int minIndex = i;
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[minIndex] > arr[j]) {
                    minIndex = j;
                }
                swap(arr, i, minIndex);
            }
        }
    }

    public static void swap(int[] arr, int i, int j) {
        int temp;
        temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }
}
